{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form gg-form-bar border-t border-x">

    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="name" placeholder="客户编码/客户名称/单号" class="layui-input" autocomplete="off" />
    </div>

    <div class="layui-input-inline" style="width:300px;">
        <select name="status" >
            <option value="">所有状态</option>
            {foreach ['','草稿','已确认']  as $key=>  $v}
            <option value="{$key}">{$v}</option>
            {/foreach}
        </select>
    </div>
    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="orderDate" placeholder="单据日期" readonly id="orderDate" class="layui-input" autocomplete="off" />
    </div>
    <div class="layui-input-inline" style="width:150px;">
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
        <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
    </div>
</form>
<div class="p-3">
    <div>
        <table class="layui-hide" id="treeTable" lay-filter="treeTable"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">+ 添加承兑</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger " lay-event="dels" type="button">删除</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal " lay-event="onUse" type="button">批量使用</button>
        <button class="layui-btn layui-btn-sm layui-btn-warm " lay-event="onBack" type="button">批量撤回</button>
    </div>
</script>
<script type="text/html" id="lockBar">
    <a href="javascript:" lay-event="seeMx">{{d.useMoney}}</a>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool'];

    let statuss=['','未使用','已使用','已过期'];
    let payTypes=['','现金','银行电汇','电子承兑'];
    function gouguInit() {
        var treeTable = layui.treeTable, tool = layui.tool;
        var form =layui.form;
        layui.laydate.render({
            elem:"#orderDate",
            range:'~'
        })
        var tableIns = treeTable.render({
            elem: '#treeTable'
            , url: "index"
                ,toolbar: '#toolbarDemo'
            , tree: { // treeTable 特定属性集
                customName: {name: 'name'},
                data: {},
                view: {showIcon: false},
                async: {},
                callback: {}
            }
            // ,defaultToolbar:  ['filter',  'exports']
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left' },
                {fixed: 'left',field: 'id',title: 'ID' ,unresize: true,hide:true},
                // , {field: 'orderNum', title: '单据编号'}
                , {field: 'pName',  title: '客户'}
                , {field: 'ticketNum',  title: '票号'}
                , {field: 'honourBank',  title: '承兑银行'}
                , {field: 'honourMoney',  title: '承兑金额'}
                , {field: 'useMoney',  title: '使用金额',toolbar:'#lockBar'}
                , {field: 'unUseMoney',  title: '未使用金额'}
                , {field: 'endTime',  title: '到期日期'}
                , {field: 'status',  title: '状态',templet:function (d) {
                        return statuss[d.status];
                    }}
                , {field: 'honourName',  title: '登记人'}
                , {field: 'crateTime',  title: '登记日期'}

                // , {title: '操作', align: 'center'
                //     , templet: function (d) {
                //         var html = '' +
                //             '<button class="layui-btn layui-btn-normal  layui-btn-xs" lay-event="edit">编辑</button>' +
                //             '';
                //         return html;
                //     }
                // }
            ]]
            , page: false
        });
        //表头工具栏事件
        $('.add-menu').on('click', function () {
            tool.side("add");
            return;
        });
        form.on('submit(webform)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //操作按钮
        treeTable.on('tool(treeTable)', function (obj) {
            console.log(obj);
            // if (obj.event === 'add') {
            //     tool.side('add?pid=' + obj.data.id);
            //     return;
            // }
            // if (obj.event === 'edit') {
            //     tool.side('add?id=' + obj.data.id);
            //     return;
            // }
            // if (obj.event === 'del') {
            //     layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
            //         let callback = function (e) {
            //             layer.msg(e.msg);
            //             if (e.code == 0) {
            //                 obj.del();
            //             }
            //         }
            //         tool.delete("delete", {id: obj.data.id}, callback);
            //         layer.close(index);
            //     });
            // }

            if (obj.event === 'seeMx') {
                    let url="seeMx?orderId="+obj.data.id;
                    tool.side(url)

            }
        });
        treeTable.on('toolbar(treeTable)', function(obj){
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                    tool.side("{:url('add')}");
                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            tool.delete("{:url('delete')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'onUse':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要使用吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            httpRequest("{:url('onUse')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'onBack':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要撤回吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            httpRequest("{:url('onBack')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;
                case 'plqr':

                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        $.ajax({
                            url:"{:url('onSure')}",
                            type:'post',
                            dataType:'json',
                            data:{id:ids.join(',')},
                            success:function (res) {
                                console.log('res',res)
                                layer.msg(res.msg)
                                if (res.code == 0) {
                                }
                            },
                            error(e){
                                layer.msg('网络错误，请稍后重试')
                            }
                        })
                    }else {
                        layer.msg('请选择数据');
                    }
                    break;
            };
        })

    }
</script>
{/block}
<!-- /脚本 -->